package com.authine.cloudpivot.common.extension.customized.service.impl;

import com.authine.cloudpivot.common.extension.customized.databaseobject.UserDO;
import com.authine.cloudpivot.common.extension.customized.model.User;
import com.authine.cloudpivot.common.extension.customized.service.CustomizedTestServiceI;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 二开扩展demo
 */
@Service
public class CustomizedTestServiceImpl implements CustomizedTestServiceI {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<String> listName() {
        List<String> list = Lists.newArrayList();
        list.add("张三");
        list.add("李四");
        list.add("王二");
        list.add("麻子");
        return list;
    }

    @Override
    public List<User> list() {
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from h_customized_user");
        List<User> users = Lists.newArrayList();
        for (Map<String, Object> map : list) {
            User user = new User();
            user.setAge(Integer.valueOf(String.valueOf(map.get("age"))));
            user.setName(String.valueOf(map.get("name")));
            user.setId(String.valueOf(map.get("id")));
            users.add(user);
        }
        return users;
    }

    @Override
    public User create(User user) {
        user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        UserDO dbUser = new UserDO();
        BeanUtils.copyProperties(user, dbUser);
        return user;
    }

    @Override
    public User update(User user) {
        LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDO::getId, user.getId());
        //查询是否存在
        //更新
        return user;
    }

    @Override
    public Integer delete(String id) {
        LambdaQueryWrapper<UserDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserDO::getId, id);
        //查询是否存在
        //删除
        return 0;
    }

}
